package com.medical.literature.document.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * WebSocket服务类，用于发送实时更新通知
 */
@Slf4j
@Service
public class WebSocketService {
    
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    
    /**
     * 发送文献状态更新通知
     * @param literatureId 文献ID
     */
    public void sendLiteratureStatusUpdate(Long literatureId) {
        log.info("发送文献状态更新通知，文献ID: {}", literatureId);
        try {
            // 发送消息到/topic/literature-status主题
            messagingTemplate.convertAndSend("/topic/literature-status", literatureId.toString());
            log.info("文献状态更新通知发送成功，文献ID: {}", literatureId);
        } catch (Exception e) {
            log.error("发送文献状态更新通知失败，文献ID: {}", literatureId, e);
        }
    }
    
    /**
     * 发送图表数据更新通知
     */
    public void sendChartUpdateNotification() {
        log.info("发送图表数据更新通知");
        try {
            // 发送消息到/topic/chart-update主题
            messagingTemplate.convertAndSend("/topic/chart-update", "update");
            log.info("图表数据更新通知发送成功");
        } catch (Exception e) {
            log.error("发送图表数据更新通知失败", e);
        }
    }
}